package lovo.practice_7_14;

import lovo.Util.Util;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class LoginPanel extends JPanel implements ActionListener {
    private J189_7_14 frame;
    private JButton jButtonLogin;
//    private Icon ImageLogin = Util.getimagesIcon("/Images/buttonLand.jpg");

    public LoginPanel(J189_7_14 frame) {
        this.frame = frame;
        this.setLayout(null);
        this.setOpaque(false);
        jButtonLogin = new JButton("欢迎你");
        jButtonLogin.setFont(new Font("宋",Font.BOLD,20));
        jButtonLogin.setBounds(400, 500, 140, 50);
        this.add(jButtonLogin);
        jButtonLogin.addActionListener(this);

    }
    /**
     * 修改数据库的值
     */
    public void cha() {

        // 1.创建连接
        Connection con = frame.getConnection();
        Statement statement = null;

        try {
            // 2 获取执行SQL语句对象
            statement = con.createStatement();
            String str=frame.getUsers().getAccount();
            String sql = " update t_atm set a_balance =" + frame.getUsers().getBalance() + " where a_account='"+str+"' ";

            //执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响行数：" + row);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //4 关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }


        }
    }
    // 转账修改被转者的数据
    public void tra() {

        // 1.创建连接
        Connection con = frame.getConnection();
        Statement statement = null;
        String sql = " update t_atm set a_balance =" +  frame.getListusers().get(frame.getI()).getBalance() + " where a_account='"+frame.getListusers().get(frame.getI()).getAccount()+"' ";
        System.out.println(sql);
        try {
            // 2 获取执行SQL语句对象
            statement = con.createStatement();

            //执行SQL语句
            int row = statement.executeUpdate(sql);
            System.out.println("影响行数：" + row);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //4 关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }


        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if (jButtonLogin == e.getSource()) {
            frame.switchPanel("scan");
        }
    }
}
